package com.sasken.pdb.models.columnmodels
{
	import flash.display.DisplayObjectContainer;
	
	import mx.containers.VBox;
	import mx.controls.CheckBox;
	import mx.controls.advancedDataGridClasses.AdvancedDataGridColumn;
	
	/**
	 *  This class is a base class for column models. Column models are objects that 
	 *  contain AdvancedDataGridColumn objects with their contents set up so that 
	 *  they can be passed through getter functions to an AdvancedDataGrid instance.
	 *  
	 * <p>There are two types of columns that a column model provides. Header columns are 
	 * used by the data grids which are not horizontally scrollable and statically 
	 * positioned on the left side of the UI. For example, "Name" column in Project View
	 * is a header column.</p>
	 * <p>Content column are the actual content of the main data grid. They are otherwise similar
	 * to header columns, but simply have different content.</p>
	 * <p>Both colum types remember their own order.</p>
	 */
	public class ColumnModel
	{
		/**
		 *  List of header columns in their default order.
		 */
		protected var _headerColumns:Array;
		/**
		 *  List of content columns in their default order.
		 */
		protected var _contentColumns:Array;
		/**
		 *  List of index numbers indicating in which order the content columns are displayed. The index numbers 
		 *  are linked to the _contentColumns' indices.
		 */
		protected var contentColumnOrder:Array;
		/**
		 *  List of index numbers indicating in which order the content columns are displayed. The index numbers 
		 *  are linked to the _headerColumns' indices.
		 */
		protected var headerColumnOrder:Array;
		
		/**
		 *  
		 */
		private var _orderedContentColumns:Array;
		
		/**
		 *  
		 */
		private var _orderedHeaderColumns:Array;
		private var _columnDisplayCheckboxes:DisplayObjectContainer;
		
		private var changedOrder:Boolean;
		
		private var _filterStrings:Array;
		
		/**
		 * 
		 */ 
		protected var _globalFilters:Array;
		
		/**
		 *  Contains all data fields so they can be used in search.
		 */ 
		protected var _dataFields:Array;
		
		/**
		 *  Constructor, initializes an object instance.
		 */ 
		public function ColumnModel()
		{
			changedOrder = true;
					
			_contentColumns = new Array();
			_headerColumns = new Array();
			
			_orderedHeaderColumns = new Array();
			_orderedContentColumns = new Array();
			
			_filterStrings = new Array();
			_globalFilters = new Array();
			_dataFields = new Array();
		}
		
		/**
		 * Returns an ordered array of header columns.
		 * 
		 *  @return An array containing the header columns in the order specified by headerColumnOrder.
		*/
		public function get headerColumns():Array
		{
			if (changedOrder)
			{	
					
				for(var i:int = 0; i < headerColumnOrder.length; ++i)
				{
					_orderedHeaderColumns[i] = _headerColumns[headerColumnOrder[i]];
				}
				//changedOrder = false;
			}
			
			return _orderedHeaderColumns;
			
		}
		
		/**
		 * Returns an ordered array of content columns.
		 * 
		 *  @return An array containing the content columns in the order specified by contentColumnOrder.
		*/
		public function get contentColumns():Array
		{
			if (changedOrder)
			{	
					
				for(var i:int = 0; i < contentColumnOrder.length; ++i)
				{
					_orderedContentColumns[i] = _contentColumns[contentColumnOrder[i]];
				}
				//changedOrder = false;
			}
			
			return _orderedContentColumns;
		}
		
		/**
		 * @returns a container with checkboxes as its children.
		 */ 
		public function get columnDisplayCheckboxes():DisplayObjectContainer
		{
			if (!_columnDisplayCheckboxes)
			{
				_columnDisplayCheckboxes= new VBox();
				
				for (var i:int = 0; i < _contentColumns.length; ++i)
				{
					var tmpChkBox:CheckBox = new CheckBox();
					tmpChkBox.id = "chkDisplayColumn" + i;
					tmpChkBox.label = (_contentColumns[i] as AdvancedDataGridColumn).headerText;
					tmpChkBox.data = i;
					tmpChkBox.selected = (_contentColumns[i] as AdvancedDataGridColumn).visible;
					_columnDisplayCheckboxes.addChild(tmpChkBox);			
				}
			}
			
			return _columnDisplayCheckboxes;
		}
		
		/**
		 *  Returns the data provider for each column data field. Returns null if no provider is available.
		 */ 
		public function getDataProviderForColumnDataField(columnDataField:String):Object
		{
			// stub, must be implemented in extended classes 
			return null;
		}
		
		public function getTitleForColumn(columnDataField:String):String
		{
			// stub, must be implemented in extended classes 
			return null;
		}
		
		public function setFilterStringForColumnDataField(columnDataField:String, filter:String):void
		{
			//trace("ColumnModel: setting filter");
			//trace(columnDataField, filter);
			_filterStrings[columnDataField] = filter.toLowerCase();
		}
		
		/**
		 *  Sets a global filter for all fields (generic search)
		 */ 
		public function setGlobalFilter(filter:String):void
		{			
			var regExp:RegExp = /\S+/gi;
			_globalFilters = filter.match(regExp);	
		}
		
		/**
		 * Returns currently used filter strings for each column as a lookup array.
		 * 
		 */ 
		protected function get filterStrings():Array
		{
			return _filterStrings;
		}

		
		public function get filterFunction():Function
		{
			return filterFunc;
		}
		
		/**
		 * Stub, implement in subclass.
		 */  
		protected function filterFunc(item:Object):Boolean
		{
			return true;
		}
		
	}
}
